package org.fileCopier.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;


public class FileCopier {
	public void copy(){
		
	}
	public static boolean copyFolder(File srcFolder ,File destFolder){
		if(srcFolder.isDirectory() && destFolder.isDirectory()){
			ArrayList<File> fileList=getAllFiles(srcFolder);
			copyAllFilestoDest(fileList,destFolder); 
			return true;
		}
		return false;		
	}
	private static void copyAllFilestoDest(ArrayList<File> fileList,
			File destFolder) {
		for (File file : fileList) {
			
		}
	}
	private static ArrayList<File> getAllFiles(File srcFolder) {
		ArrayList<File> fileList=new ArrayList<File>();
		populateFileList(srcFolder, fileList);
		return fileList;		
	}
	private static void populateFileList(File srcFolder,ArrayList<File> fileList){
		if(fileList==null)
			fileList=new ArrayList<File>();
		File[] listFiles = srcFolder.listFiles();
		for (File file : listFiles) {
			if(!file.isDirectory())
				fileList.add(file);
			else{
				populateFileList(file,fileList);
			}
		}		
	}
	public static void copyFile(File sourceFile, File destFile) throws IOException {
		 if(!destFile.exists()) {
		  destFile.createNewFile();
		 }		
		 FileChannel source = null;
		 FileChannel destination = null;
		 try {
		  source = new FileInputStream(sourceFile).getChannel();
		  destination = new FileOutputStream(destFile).getChannel();
		  destination.transferFrom(source, 0, source.size());
		 }
		 finally {
		  if(source != null) {
		   source.close();
		  }
		  if(destination != null) {
		   destination.close();
		  }
		}
	}
}
